package com.ztgf.sms.api.cms.sms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import com.ztgf.sms.models.service.ISmsPassagewayService;
import com.ztgf.sms.models.bo.SmsPassagewaySaveBO;
import com.ztgf.sms.models.bo.SmsPassagewayBO;
import com.ztgf.sms.models.dto.sms.SmsPassagewayDTO;

/**
 * 短信通道管理表控制器
 *
 * @author chenyu.wang
 * @Date 2021-01-27 11:55:15
 */

@RestController
@RequestMapping("/cms/smsPassageway")
@Slf4j
@Api(value = "SmsPassagewayController", tags = "短信通道管理表接口")
public class SmsPassagewayController {

    @DubboReference
    private ISmsPassagewayService smsPassagewayService;

    @ApiOperation(value = "添加短信通道管理表", notes = "添加短信通道管理表")
    @PostMapping(value = "/insert")
    public Result<?> insert(SmsPassagewaySaveBO saveBO) {
        boolean flag = smsPassagewayService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改短信通道管理表", notes = "修改短信通道管理表")
    public Result<?> update(SmsPassagewaySaveBO saveBO) {
        SmsPassagewayDTO dto = new SmsPassagewayDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = smsPassagewayService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除短信通道管理表", notes = "删除短信通道管理表")
    @ApiImplicitParam(name = "id", value = "短信通道管理表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result<?> delete(Long id) {
        SmsPassagewayDTO smsPassagewayDTO = smsPassagewayService.selectById(id);
        if (smsPassagewayDTO==null){
            return new Result<>(CodeEnum.FAIL_100009);
        }
        smsPassagewayDTO.setIsDelete(1);
        boolean flag = smsPassagewayService.updateById(smsPassagewayDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除短信通道管理表", notes = "批量删除短信通道管理表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result<?> deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        smsPassagewayService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SmsPassagewayDTO> get(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SmsPassagewayDTO dto = smsPassagewayService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "短信通道管理表列表", notes = "短信通道管理表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SmsPassagewayDTO>> queryPageList(SmsPassagewayBO bo) {
        Wrapper<SmsPassagewayDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_delete",0);
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());
        }
        Page<SmsPassagewayDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = smsPassagewayService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @PostMapping("/queryList")
    @ApiOperation(value = "已启用的通道列表", notes = "已启用的通道列表")
    public Result<?> queryList() {
        Wrapper<SmsPassagewayDTO> wrapper = new EntityWrapper<>();
       // wrapper.eq("state", 0);
        //wrapper.eq("is_delete",0);
        List<SmsPassagewayDTO> list = smsPassagewayService.selectList(wrapper);
        return new Result<>(list);
    }

}
